import { SKILL_NUM, TeamOrder } from "../../config/play_def";
import { Control_Listen } from "../../config/ui_def";
import { getLongScreen } from "../tool/util";

cc.Class({
    extends: cc.Component,

    properties: {
        layout: cc.Layout,
        widget: cc.Widget,
    },
    initPage() {
        let skill_prefab = he.load.get('uimod/skillButton')
        let user_team = he.local.getJson('card_team', 'user')   // 卡牌队伍
        let skill_width = 0
        this.initPublicCd()     // 初始化公共技能cd
        for (let i = 0; i < SKILL_NUM; i++) {
            let skill_node = cc.instantiate(skill_prefab)
            if (!i) {
                skill_width = skill_node.width
            }
            skill_node.name = 'skill_' + TeamOrder[i]
            this.node.addChild(skill_node)

            let skill_button = skill_node.getComponent('skillButton')
            skill_button.skillBox = this
            skill_button.initInfo()
        }

        for (let i = 0; i < user_team.length; i++) {
            let skill_node = this.node.getChildByName("skill_" + user_team[i].index)
            let skill_button = skill_node.getComponent('skillButton')
            skill_button.initInfo(user_team[i])
        }
        this.layout.spacingX = ((this.layout.node.width - (this.layout.paddingLeft + this.layout.paddingRight)) - skill_width * SKILL_NUM) / (SKILL_NUM - 1)
        this.layout.updateLayout()
    },
    updatePage() {
        if(getLongScreen()){
            this.widget.bottom = 50
        }else{
            this.widget.bottom = 100
        }
        this.widget.updateAlignment()
        gg.eventManager.on("skill", this.listenClient, this)
    },
    //初始化公用CD
    initPublicCd(){
        // he.log("初始化公用CD")
        this.publicCd = 0
    },
    clearSkillCd(){
        let user_team = he.local.getJson('card_team', 'user')   // 卡牌队伍
        for (let i = 0; i < user_team.length; i++) {
            let skill_node = this.node.getChildByName("skill_" + user_team[i].index)
            let skill_button = skill_node.getComponent('skillButton')
            skill_button.restartCd()
        }
    },
    // 监听
    listenClient(code, param, arg) {
        if (code) {
            switch (code) {
                case Control_Listen.SKILL_CD_CLEAR:
                    this.clearSkillCd()
                    break
            }
        }
    },
    closeUI() {

    },
    update(dt){
        this.updateCd(dt);
    },
    updateCd(dt){
        if (this.publicCd<=0) {
            return;
        }
        this.publicCd -= dt
    }
});
